/*
* @Author: mmall
* @Date:   2017-05-27 17:57:49
* @Last Modified by:   Rosen
* @Last Modified time: 2017-05-28 19:48:16
*/

'use strict';
require('./index.css');
require('page/common/nav/index.js');
require('page/common/headerMultipleConditions/index.js');
var _mm = require('util/mm.js');
var _product = require('service/product-service.js');
var Pagination = require('util/pagination/index.js');
var templateIndex = require('./index.string');

var area = "";
var maxPrice = "";
var brand = ""
var specs = ""
var minPrice = ""
var sortWay = ""
var page = {
    data: {
        listParam: {
            condition: _mm.getUrlParam('condition') || null,
            area: _mm.getUrlParam('area') || null,
            maxPrice: _mm.getUrlParam('maxPrice') || null,
            brand: _mm.getUrlParam('brand') || null,
            specs: _mm.getUrlParam('specs') || null,
            minPrice: _mm.getUrlParam('minPrice') || null,
            sortWay: _mm.getUrlParam('sortWay') || null,
            // categoryId      : _mm.getUrlParam('categoryId') || '',
            current: _mm.getUrlParam('pageNum') || 1,
            pageSize: _mm.getUrlParam('pageSize') || 10,
        }
    },
    init: function () {
        this.onLoad();
        this.bindEvent();
    },
    onLoad: function () {
        this.loadList();
    },
    bindEvent: function () {
        var _this = this;
        // 排序的点击事件
        $('.sort-item').click(function () {
            var $this = $(this);
            _this.data.listParam.current = 1;
            $this.addClass('active').siblings('.sort-item')
                .removeClass('active asc desc');
            _this.data.listParam.brand = $this.data('brand');
            // 重新加载列表
            _this.loadList();
        });

        $('#but_area').click(function () {
            _this.data.listParam.pageNum = 1;
            _this.data.listParam.area = $.trim($('#area').val())
            // 重新加载列表
            _this.loadList();
        });
        $('.sort-region').click(function () {
            var $this = $(this);
            _this.data.listParam.current = 1;
            $this.addClass('active').siblings('.sort-region')
                .removeClass('active asc desc');
            _this.data.listParam.area = $this.data('area');
            // 重新加载列表
            _this.loadList();
        });
        $('#but_price').click(function () {
            _this.data.listParam.pageNum = 1;
            _this.data.listParam.minPrice = $.trim($('#minPrice').val())
            _this.data.listParam.maxPrice = $.trim($('#maxPrice').val())
            // 重新加载列表
            _this.loadList();
        });
        $('.sort-specs').click(function () {
            var $this = $(this);
            _this.data.listParam.current = 1;
            $this.addClass('active').siblings('.sort-specs')
                .removeClass('active asc desc');
            _this.data.listParam.specs = $this.data('specs');
            // 重新加载列表
            _this.loadList();
        });
        // 排序的点击事件
        $('.sort-salesVolume').click(function () {
            var $this = $(this);
            _this.data.listParam.pageNum = 1;
            // 点击默认排序
            if ($this.data('type') === 'default') {
                // 已经是active样式
                if ($this.hasClass('active')) {
                    return;
                }
                // 其他
                else {
                    $this.addClass('active').siblings('.sort-salesVolume')
                        .removeClass('active asc desc');
                    _this.data.listParam.sortWay = 'default';
                }
            }
            // 点击价格排序
            else if ($this.data('type') === 'salesVolume') {
                // active class 的处理
                $this.addClass('active').siblings('.sort-item')
                    .removeClass('active asc desc');
                // 升序、降序的处理
                if (!$this.hasClass('asc')) {
                    $this.addClass('asc').removeClass('desc');
                    _this.data.listParam.sortWay = 'asc';
                } else {
                    $this.addClass('desc').removeClass('asc');
                    _this.data.listParam.sortWay = 'desc';
                }
            }
            // 重新加载列表
            _this.loadList();
        });

    },
    // 加载list数据
    loadList: function () {
        var _this = this,
            listHtml = '',
            listParam = this.data.listParam,
            $pListCon = $('.p-list-con');

        $pListCon.html('<div class="loading"></div>');
        // 删除参数中不必要的字段
        listParam.categoryId
            ? (delete listParam.keyword) : (delete listParam.categoryId);
        // 请求接口
        _product.getSearchBoolQueryList(listParam, function (res) {
            listHtml = _mm.renderHtml(templateIndex, {
                list: res.list
            });
            $pListCon.html(listHtml);
            var totalResultSize = res.totalResultSize;
            var totalPage = res.totalPage;
            var currentPage = res.current;
            area = res.area;
            maxPrice = res.maxPrice;
            brand = res.brand
            specs = res.specs
            minPrice = res.minPrice
            sortWay = res.sortWay
            _this.loadPagination({
                hasPreviousPage: currentPage > 1,
                prePage: currentPage - 1,

                hasNextPage: currentPage < totalPage,
                nextPage: currentPage + 1,

                pageNum: currentPage,
                pages: totalPage
            });
        }, function (errMsg) {
            _mm.errorTips(errMsg);
        });
    },
    // 加载分页信息
    loadPagination: function (pageInfo) {
        var _this = this;
        this.pagination ? '' : (this.pagination = new Pagination());
        this.pagination.render($.extend({}, pageInfo, {
            container: $('.pagination'),
            onSelectPage: function (pageNum) {
                _this.data.listParam.current = pageNum;
                _this.data.listParam.area = area;
                _this.data.listParam.maxPrice = maxPrice;
                _this.data.listParam.brand = brand;
                _this.data.listParam.specs = specs;
                _this.data.listParam.minPrice = minPrice;
                _this.data.listParam.sortWay = sortWay;
                _this.loadList();
            }
        }));
    }
};
$(function () {
    page.init();
})